//
//  CWQEvaluationView.m
//  BaxTer
//
//  Created by 陈武琦 on 16/10/11.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "CWQEvaluationView.h"
#import "HCSStarRatingView.h"
@implementation CWQEvaluationView

+ (CWQEvaluationView *)evaluationView {

    CWQEvaluationView *view =  [[NSBundle mainBundle] loadNibNamed:@"CWQEvaluationView" owner:nil options:0].lastObject;
    [view.helpStarRatingView addObserver:view forKeyPath:@"value" options:NSKeyValueObservingOptionNew context:nil];
    [view.contentStarRatingView addObserver:view forKeyPath:@"value" options:NSKeyValueObservingOptionNew context:nil];
    

    return view;
}

- (void)dealloc{
    [self.helpStarRatingView removeObserver:self forKeyPath:@"value"];
    [self.contentStarRatingView removeObserver:self forKeyPath:@"value"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"value"]) {
        NSNumber *value =  change[@"new"];
        if (value.floatValue < 0.5) {
            value = [NSNumber numberWithInt:0];
        }
        value = @([value floatValue] *2);
        
        if ([object isEqual:self.helpStarRatingView]) {
            self.helpScore.text = [[value stringValue]  stringByAppendingString:@"分"];
        }else {
            self.contentScore.text = [[value stringValue]  stringByAppendingString:@"分"];
        }
    
        if (self.helpStarRatingView.value <=0 && self.contentStarRatingView.value <=0) {
            self.sureBtn.backgroundColor = [UIColor grayColor];
             self.sureBtn.enabled = NO;
           
        }else{
            self.sureBtn.backgroundColor = [UIColor orangeColor];
            self.sureBtn.enabled = YES;

        }
        
    }

  


}

- (IBAction)sureClick:(id)sender {
    self.hidden = YES;
    WSJLog(@"__________%f",self.helpStarRatingView.value);
    WSJLog(@"++++++++++%f",self.contentStarRatingView.value);
    if (self.reloadView) {
        self.reloadView(self.helpStarRatingView.value * 2,self.contentStarRatingView.value *2);
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
